#!/bin/bash

set -o errexit
set -o nounset
set -o pipefail

KIND_IMAGE="kindest/node:v1.28.0"
CLUSTER_NAME="cka-exam-env"

update_sources() {
    local node_name="$1"

    local commands=$(cat <<EOF
cp /etc/apt/sources.list /etc/apt/sources.list.backup &&
echo "deb http://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm main contrib non-free non-free-firmware
# deb-src http://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm main contrib non-free non-free-firmware

deb http://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm-updates main contrib non-free non-free-firmware
# deb-src http://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm-updates main contrib non-free non-free-firmware

deb http://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm-backports main contrib non-free non-free-firmware
# deb-src http://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm-backports main contrib non-free non-free-firmware

deb https://mirrors.tuna.tsinghua.edu.cn/debian-security bookworm-security main contrib non-free non-free-firmware
# deb-src https://security.debian.org/debian-security bookworm-security main contrib non-free non-free-firmware" > /etc/apt/sources.list
EOF
)
    docker exec "${node_name}" /bin/sh -c "apt install apt-transport-https ca-certificates"
    docker exec "${node_name}" /bin/sh -c "$commands"
    docker exec "${node_name}" /bin/sh -c "apt update"
    docker exec "${node_name}" /bin/sh -c "apt-get install -y vim inetutils-ping sudo apt-transport-https gnupg wget"
}

function update_kubernetes_sources() {
    local node_name=$1
    #目前阿里云维护的版本最新只到1.28，按照题目要求最低得到1.29.1，如果网络好的同学，可以参考官网，将下面链接替换成官网链接
    docker exec "${node_name}" /bin/sh -c 'echo "deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main" > /etc/apt/sources.list.d/kubernetes.list'
    docker exec "${node_name}" /bin/sh -c "curl -fsSL https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | apt-key add -"
    docker exec "${node_name}" /bin/sh -c "apt update"
}

kind delete clusters $CLUSTER_NAME || true
kind create cluster --name $CLUSTER_NAME --config kind_cluster.yml --image $KIND_IMAGE

#TODO create ns foo for Q5
#TODO touch /opt/KUTR000402/KUTR00402.txt in node1

update_sources "cka-exam-env-control-plane"
update_sources "cka-exam-env-worker"
update_kubernetes_sources "cka-exam-env-control-plane"


docker pull docker.m.daocloud.io/nginx
docker tag docker.m.daocloud.io/nginx nginx
docker pull docker.m.daocloud.io/redis
docker tag docker.m.daocloud.io/redis redis
docker pull docker.m.daocloud.io/busybox
docker tag docker.m.daocloud.io/busybox busybox

kind load -n "$CLUSTER_NAME" docker-image redis
kind load -n "$CLUSTER_NAME" docker-image nginx
kubectl label node cka-exam-env-worker2 disk=spinning

docker pull k8s.m.daocloud.io/metrics-server/metrics-server:v0.7.1
docker tag k8s.m.daocloud.io/metrics-server/metrics-server:v0.7.1 registry.k8s.io/metrics-server/metrics-server:v0.7.1
kind load -n "$CLUSTER_NAME" docker-image registry.k8s.io/metrics-server/metrics-server:v0.7.1

kubectl create ns app-team1

#准备第5题的环境
kubectl create ns corp-net

kubectl apply -f Q2_prepare.yml
kubectl apply -f Q6_prepare.yml
kubectl apply -f Q7_prepare.yml
kubectl apply -f Q8_prepare.yml
kubectl apply -f Q14_prepare.yml
kubectl apply -f Q15_prepare.yml



#第十三题环境
#TODO 需要找一个支持修改容量的csi插件，同时将sc的名字改成与题目一样，比较麻烦，后面研究补上

kubectl apply -f metrics-server.yml
kubectl apply -f Q16_prepare.yml





